package testProducerAndConsumer;

public class Store {
    private final int MAX=10;
    private int size=0;

    public synchronized void make() throws InterruptedException {
        String name = Thread.currentThread().getName();
        while (this.size>=MAX){
            System.out.println("当前库存：" + this.size + "，达到库存上线，" + name + "等待消费者消费");
            wait();
        }
        this.size++;
        System.out.println(name + "生产一件产品，当前库存：" + this.size);
        Thread.sleep(100);
        notifyAll();
    }

    public synchronized void sell() throws InterruptedException {
        String name = Thread.currentThread().getName();
        while (this.size<=0){
            System.out.println("当前库存："+ this.size +"，" + name + "等待生产者生产");
            wait();
        }
        this.size--;
        System.out.println(name + "消费一件产品，当前库存：" + this.size);
        notifyAll();
    }
}
